package com.nbx.app.neheport;

import com.nbx.app.neheport.render.*;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;

/**
 * Wrapper activity demonstrating the use of {@link GLSurfaceView}, a view that
 * uses OpenGL drawing into a dedicated surface.
 */
public class GLSurfaceViewActivity extends Activity {

    private RenderBase mCurrentRender = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Renderer render = null;
        Bundle bundle = this.getIntent().getExtras();

        int index = bundle.getInt("INDEX");
        
        switch (index) {
        default:
        case  0: render = new Render01(this);  break;
        case  1: render = new Render02(this);  break;   
        case  2: render = new Render03(this);  break;
        case  3: render = new Render04(this);  break;
        case  4: render = new Render05(this);  break;
        case  5: render = new Render06(this);  break;
        case  6: render = new Render07(this);  break;
        case  7: render = new Render08(this);  break;
        case  8: render = new Render09(this);  break;
        case  9: render = new Render10(this);  break;
        case 10: render = new Render11(this);  break;
        case 11: render = new Render12(this);  break;
        case 12: render = new Render13(this);  break;
        
        case 14: render = new Render15(this);  break;
        case 15: render = new Render16(this);  break;
        case 16: render = new Render17(this);  break;
        case 17: render = new Render18(this);  break;
        case 18: render = new Render19(this);  break;
        
        }

        if (render != null) {
            mCurrentRender = (RenderBase) render;
            mGLSurfaceView = new GLSurfaceView(this);
            mGLSurfaceView.setRenderer(render);
            setContentView(mGLSurfaceView);

            String title = bundle.getString("TITLE");
            setTitle(title);
        }
    }

    @Override
    protected void onResume() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onResume();
        mGLSurfaceView.onResume();
    }

    @Override
    protected void onPause() {
        // Ideally a game should implement onResume() and onPause()
        // to take appropriate action when the activity looses focus
        super.onPause();
        mGLSurfaceView.onPause();
    }

    private GLSurfaceView mGLSurfaceView;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mCurrentRender != null) {
            boolean bRet = mCurrentRender.handleTouchEvent(event);
            if (bRet) {
                return true;
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (mCurrentRender != null) {
            boolean bRet = mCurrentRender.handleKeyEvent(keyCode, event);
            if (bRet) {
                // mGLSurfaceView.requestRender();
                return true;
            }
        }
        return super.onKeyUp(keyCode, event);
    }
}
